//
//  GoodsStarsView.swift
//  HoneyBee
//
//  Created by point on 2018/3/25.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

class GoodsStarsView: UIView {
    
    fileprivate lazy var starsView:[UIImageView] = [UIImageView]()
    fileprivate var marginX:CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        marginX = (self.width - (20 * 5)) / 6
        setupUI()
        self.backgroundColor = UIColor.white
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension GoodsStarsView {
    
    func selectIndex(currentInt:Int) {
        for index in 0...4 {
            let view = starsView[index ]
            view.image = UIImage(named: "product_star_hui")
        }
        
        for index in 0...currentInt {
            print(index,"hong")
            let view = starsView[index]
            view.image = UIImage(named: "product_star_hong")
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let pt: CGPoint = touches.first!.location(in: self)
        self.showStars(x: pt.x)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let pt: CGPoint = touches.first!.location(in: self)
        self.showStars(x: pt.x)
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        // let pt: CGPoint = touches.first!.location(in: self)
        // self.showStars(x: pt.x)
    }
    
    func showStars(x:CGFloat)  {
        if x <= 0 {
            for view: UIImageView in starsView {
                view.image = UIImage(named: "product_star_hui")
            }
            return
        }
        
        if x >= self.width {
            for view: UIImageView in starsView {
                view.image = UIImage(named: "product_star_hong")
            }
            return
        }
        
        
        if (x > 0 && x < self.width) {
            let currentInt = Int( x   / (20 + marginX) )
            
            print(currentInt)
            if currentInt > 4 {return }
            
            
            //            if currentInt != 4 {
            for index in 0...4 {
                //    print(index,"hui")
                let view = starsView[index ]
                view.image = UIImage(named: "product_star_hui")
            }
            
            //
            
            for index in 0...currentInt {
                print(index,"hong")
                let view = starsView[index]
                view.image = UIImage(named: "product_star_hong")
            }
            
            
        }
    }
}

extension GoodsStarsView {
    fileprivate func setupUI(){
        for index in 0...4 {
            let sView = UIImageView()
            let i = CGFloat(index)
            let w:CGFloat = 20
            let h:CGFloat = 20
            let x:CGFloat = marginX + (i * w) + (i * marginX)
            let y:CGFloat = 0
            sView.frame = CGRect(x: x, y: y, width: w, height: h)
            sView.image = UIImage(named: "product_star_hui")
            self.addSubview(sView)
            starsView.append(sView)
        }
    }
}
